<?php
require_once '../controller.php';
require_once '../header.php';


if(isset($_REQUEST['sid'])){
	$survey_id = $_REQUEST['sid'];
}

$s = new Survey();
$survey = $s->getSurvey($survey_id);
if(isset($_POST['select_group'])) {
		foreach($_POST as $key=>$value) {
			if($key != 'select_group') {
				$selected[] = $key;
			}
		}
   $group_ids = implode(',',$selected);
   $group_ids = addslashes($group_ids);
   $sql = "UPDATE `survey` SET `scheduled_to_groups` = '$group_ids' WHERE `id` = '$survey_id' ";
   $result = mysql_query($sql)or die("Error scheduling to groups".mysql_error());
  
   echo "<table>";
   echo "<form name='setmessage' method ='post'>";
   echo "<tr><th>Email-Subject:</th><td><input name='subject' type='text' value='".$survey['email_subject']."' size='50px'></td></tr>";
   echo "<tr><td>&nbsp;</td></tr>";
   echo "<tr><th>Email-Message:</th><td><textarea name='message' cols='50' rows='10'>".$survey['email_message']."</textarea></td></tr>";
   echo "<tr><td>&nbsp;</td></tr>";
   echo "<tr><td><input type='submit' name='set_message' Value='Set Delivery Message'></td></tr>";
   echo "</form>";
   echo "</table>";
  
	}

if(isset($_POST['set_message'])){
	$sql = "UPDATE `survey` SET `email_subject`= '{$_POST['subject']}', `email_message`='{$_POST['message']}'
	        WHERE `id`='$survey_id'";
	$result = mysql_query($sql) or die("Update email message:".mysql_error());
	if($result){
		 echo'Survey Scheduled Sucessfully.<br><br>';
         echo '<a href="interface.php"><font color="blue">Go Back to Interface</font></a>';
	}
}
	
	
if(isset($_REQUEST['sid'])&& !isset($_POST['select_group']) &&!isset($_POST['set_message'])) {

	$survey_id = $_REQUEST['sid'];
	echo '<form name = "deliver_form" method="POST">';
	//   echo '<select name="select">';
	print '<ul>';
	$courses = $instructor->getAllCourses();
	if($courses) {
		foreach($courses as $c) {
			print "<li>$c->title</li>";
			$groups = $c->getAllGroups();
			if($groups) {
				print '<ul>';
				foreach($groups as $g) {
					print "<li><input name='$g->id' type='checkbox'>$g->name</li>";
				}
				print '</ul>';
			}
		}
	}
	print '</ul>';
	//    for($i=0;$i<sizeof($groupName);$i++) {
	//        echo '<input type="checkbox" name="check_group_'.$i.'" value ="'.$group_id[$i].'">'.$groupName[$i].'<br>';
	//      }
	echo '<input type="submit" name = "select_group" value = "Select Group"';
	echo '</form>';
	
}	
require_once '../footer.php';

?>